﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.InteropServices;
using System.Xml;


namespace SoftPhone
{
    [ComImport, Guid("870AF99C-171D-4F9E-AF0D-E63DF40C2BC9")]
    internal class _PolicyConfigClient
    {
    }

    public class PolicyConfigClient
    {
        private readonly IPolicyConfig _PolicyConfig;

        public PolicyConfigClient()
        {
            _PolicyConfig = new _PolicyConfigClient() as IPolicyConfig;
            if (_PolicyConfig != null)
                return;
        }

        public void SetDefaultEndpoint(string devID, CoreAudioApi.ERole eRole)
        {
            if (_PolicyConfig != null)
            {
                Marshal.ThrowExceptionForHR(_PolicyConfig.SetDefaultEndpoint(devID, eRole));
                return;
            }
        }

        public static bool CheckIP(string IP, int port)
        {
            IPAddress host;
            if (IPAddress.TryParse(IP, out host))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void SaveConfig(string section, Dictionary<string, string> config, ref string message)
        {
            if (!System.IO.File.Exists("config.xml"))
            {
                CreateXML(section, config);
            }
            else
            {
                UpdateXML(section, config, ref message);
            }
        }

        private static void CreateXML(string section, Dictionary<string, string> config)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(@"config.xml", settings);

            writer.WriteStartDocument();

            writer.WriteComment("This file is generated by the program.");
            writer.WriteStartElement(section);
            foreach (var item in config)
            {
                writer.WriteElementString(item.Key, item.Value);
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        private static void UpdateXML(string section, Dictionary<string, string> config, ref string message)
        {
            try
            {
                var tmp = PolicyConfigClient.ReadConfig("Account");
                foreach (var item in config)
                {
                    tmp.Add(item.Key, item.Value);
                }
                PolicyConfigClient.CreateXML("Account", tmp);
            }
            catch
            {
                message = "Có vấn đề xảy ra trong quá trình lưu tài khoản." + "\n" + "Nếu thử nhiều lần vẫn không lưu được thì kiến nghị xóa file config.xml và thử lại.";
            }
        }


        public static Dictionary<string, string> ReadConfig(string section)
        {
            try
            {
                XmlDocument xmlFile = new XmlDocument();
                Dictionary<string, string> config = new Dictionary<string, string>();
                xmlFile.Load(@"config.xml");
                XmlNode root = xmlFile.DocumentElement;
                if (root.Name == section)

                foreach (XmlNode node in root.ChildNodes)
                {
                    config.Add(node.Name, node.InnerText);
                }

                return config;
            }
            catch
            {
                return null;
            }
            
        }

    }
}
